#include <QLabel>
#include <QGridLayout>
#include <QLineEdit>
#include <QTextEdit>
#include <QItemSelectionModel>
#include <QCheckBox>
#include "SymbolModel.h"
#include "TextEdit.h"

#include "EditSymbolView.h"

EditSymbolView::EditSymbolView( QWidget * parent ) : QWidget( parent )
{
    QGridLayout * layout = new QGridLayout( this );
    
    idLabel = new QLabel( tr("Id :") );
    idValue = new QLabel();
    
    nameLabel = new QLabel( tr("&Name :") );
    nameEdit = new QLineEdit();
    nameLabel->setBuddy( nameEdit );
    
    commentsLabel = new QLabel( tr("&Description :" ) );
    commentsEdit = new TextEdit();
    commentsLabel->setBuddy( commentsEdit );
    
    usedLabel = new QLabel( tr("&Used" ) );
    usedCheckBox = new QCheckBox();
    usedLabel->setBuddy(usedCheckBox);
    
    layout->addWidget( idLabel, 0, 0 );
    layout->addWidget( idValue, 0, 1 );
    
    layout->addWidget( nameLabel, 1, 0 );
    layout->addWidget( nameEdit, 1, 1 );
    
    layout->addWidget(usedLabel, 2, 0 );
    layout->addWidget(usedCheckBox, 2, 1 );
    
    layout->addWidget( commentsLabel, 3, 0 );
    layout->addWidget( commentsEdit, 3, 1 );
    
    selectionModel = 0;

    setLayout( layout );
    
    connect( usedCheckBox, SIGNAL( stateChanged(int)), this, SLOT( usedStateChanged(int) ) );
    connect( nameEdit, SIGNAL( editingFinished() ), this, SLOT( nameEditingFinished() ) );
    connect( commentsEdit, SIGNAL( editingFinished() ), this, SLOT( commentsEditingFinished() ) );
}

void EditSymbolView::currentChanged( const QModelIndex & current, const QModelIndex & previous )
{
    Q_UNUSED( previous );
    
    int row = current.row();
    
    idValue->blockSignals(true);
    idValue->setText( QString::number( symbolModel->data( current.sibling( row, 1 ) ).toInt() ) );
    idValue->blockSignals(false);
    
    nameEdit->blockSignals(true);
    nameEdit->setText( symbolModel->data( current.sibling( row, 0 ) ).toString() );
    nameEdit->blockSignals(false);
    
    usedCheckBox->blockSignals(true);
    usedCheckBox->setCheckState( symbolModel->data( current.sibling( row, 2 ) ).toBool() ? Qt::Checked : Qt::Unchecked );
    usedCheckBox->blockSignals(false);
    
    commentsEdit->blockSignals(true);    
    commentsEdit->setText( symbolModel->data( current.sibling( row, 3 ) ).toString() );
    commentsEdit->blockSignals(false);    
    
    currentIndex = current;
}

void EditSymbolView::usedStateChanged( int state )
{
    bool
        value;
        
    switch( state )
    {
        case Qt::Checked:
        {
            value = true;
        }
        break;
        
        case Qt::Unchecked:
        {
            value = false;
        }
        break;
        
        default:
            Q_ASSERT( !"Unsupported state" );
    };
    
    symbolModel->setData( currentIndex.sibling( currentIndex.row(), 2 ), value );
}

void EditSymbolView::nameEditingFinished()
{
    symbolModel->setData( currentIndex.sibling( currentIndex.row(), 0 ), nameEdit->text() );
}

void EditSymbolView::commentsEditingFinished()
{
    symbolModel->setData( currentIndex.sibling( currentIndex.row(), 3 ), commentsEdit->toPlainText() );
}
    
